﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;

namespace BackEnd.Controllers
{
    public class ContentsController : Controller
    {
        //
        // GET: /Content/
        [CustomAuthorize(Action = Constants.AdminActions.ListContents)]
        public ActionResult Index()
        {
            ContentModel contentModel = new ContentModel();
            contentModel.ListContent = new ContentBL().GetAll();
            return View(contentModel);
        }

        [CustomAuthorize(Action = Constants.AdminActions.CreateContents)]
        public ActionResult Create()
        {
            ContentModel contentModel = new ContentModel();
            contentModel.ListContentType = new ContentTypeBL().GetAll();
            return View(contentModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        [CustomAuthorize(Action = Constants.AdminActions.CreateContents)]
        public ActionResult Create(ContentModel contentModel)
        {
            if (string.IsNullOrWhiteSpace(contentModel.Contents))
            {
                ModelState.AddModelError("Content", "Content is not null");
            }
            if (ModelState.IsValid == false)
            {                
                contentModel.ListContentType = new ContentTypeBL().GetAll();
                return View(contentModel);
            }
            else
            {
                new ContentBL().Insert(new Content { Contents = contentModel.Contents,ContentTypeId=contentModel.ContentTypeId });
                return RedirectToAction("Index", "Contents");
            }
        }
        [CustomAuthorize(Action = Constants.AdminActions.EditContents)]
        public ActionResult Edit(int id)
        {
            ContentModel contentModel = new ContentModel();
            Content content = new ContentBL().GetById(id);
            if (content != null)
            {
                contentModel.Id = content.Id;
                contentModel.ContentTypeId = content.ContentTypeId;
                contentModel.Contents = content.Contents;
            }
            contentModel.ListContentType = new ContentTypeBL().GetAll();
            return View(contentModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        [CustomAuthorize(Action = Constants.AdminActions.EditContents)]
        public ActionResult Edit(ContentModel contentModel)
        {
            if (string.IsNullOrWhiteSpace(contentModel.Contents))
            {
                ModelState.AddModelError("Content", "Content is not null");
            }
            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new ContentBL().Update(new Content { Id = contentModel.Id, ContentTypeId = contentModel.ContentTypeId , Contents= contentModel.Contents});
                return RedirectToAction("Index", "Contents");
            }
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteContents)]
        public JsonResult Delete(string listId)
        {            
            string[] arrayId = listId.Split('_');
            for (int i = 0; i < arrayId.Length; i++)
            {
                new ContentBL().Delete(Convert.ToInt32(arrayId[i]));
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }

    }
}
